<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-11-26
 * Time: 下午2:33
 */

class PrivilegeAction extends Action
{
  /**
     * 初始化
     */
    protected $province=null;
    public function _initialize() {
      $this->province=model("Area")->where("pid=0")->select();
    }
  //后台管理员登录完以后、显示的主页面
   public function index(){
    $CityId=intval($_GET["CityId"]);
    $uname=$_GET["uname"];
    $data=$this->getUserList($CityId,$uname,1);
    $this->assign("city_user",$data);
    $this->assign("province",$this->province);
    $this->display("admin_user");
  }
    //显示新建市级用户
    public function create_city_user(){
      $this->assign("province",$this->province);
      $this->display("admin_create_city");
    }
    //显示区级用户
    public function show_area_user(){
      $City=intval($_GET["City"]);
      $uname=$_GET["uname"];
      $data=$this->getUserList($City,$uname,2);
      $this->assign("area_user",$data);
      $this->assign("province",$this->province);
      $this->display("admin_area");
    }
    //新建区级用户
    public function create_area_user(){
      $this->assign("province",$this->province);
      $this->display("admin_create_area");
    }
  //显示园级用户
  public function show_garden_user(){
    $AreaId=intval($_GET["AreaId"]);
    $uname=$_GET["uname"];
    $data=$this->getUserList($AreaId,$uname,3);
    foreach ($data['data'] as $key => $value) {
      $data['data'][$key]["school"]=M("school")->where("fid=".$value["uid"])->field("type,location")->find();
    }
    $this->assign("garden_user",$data);
    $this->assign("province",$this->province);
    $this->display("admin_garden");
  }
  //新建园级用户
  public function create_garden_user(){
    $this->assign("province",$this->province);
    $this->display("admin_create_garden");
  }
    //添加用户
    public function addUser(){
      $usertype=intval($_POST["usertype"]);
      $email=getAllLetter($_POST["uname"]);
      if(model("User")->where("permissions=1 and email='".$email."'")->find()){
        $email=$email."_1";
      }
      $login_salt=rand(10000, 99999);
      $data=array(
        'location'=>$_POST["location"],
        'province'=>$_POST["Province"],
        'city'=>$_POST["City"],
        'uname'=>$_POST["uname"],
        'email'=>$email,
        'pwd'=>$_POST["pwd"],
        'ctime'=>time(),
        'login_salt'=>$login_salt,
        'password'=>md5 (md5($_POST["pwd"]).$login_salt),
		    'no_login'=>1,
        'permissions'=>1,
      );
      if($usertype!=2){
        $data['area']=$_POST["Area"];
        if($usertype==3){
          $uid=M("user")->where("city=".$_POST["City"])->field("uid")->select();
          $uid = getSubByKey($uid,'uid');
          $where=array(
            'uid'=>array('in' , $uid),
            'user_group_id'=>2
          );
          $userGroup=model("UserGroupLink")->where($where)->find();
          if($userGroup){
            $data['fid']=$userGroup['uid'];
          }else{
            echo "还没有该市的市级账号";exit();
          }
        }
        if($usertype==4){
          $data['permissions']=1;
          $uid=M("user")->where("area=".$_POST["Area"])->field("uid")->select();
          $uid = getSubByKey($uid,'uid');
          $where=array(
            'uid'=>array('in' , $uid),
            'user_group_id'=>3
          );
          $userGroup=model("UserGroupLink")->where($where)->find();
          if($userGroup){
            $data['fid']=$userGroup['uid'];
          }else{
            echo "还没有该地区的区级账号";exit();
          }
        }
      }else{
        $data['fid']=$this->mid;
      }
      if(M("User")->where("uname=".$_POST["uname"])->find()){
        echo "此名称已存在";exit();
      }
      $res=M("User")->add($data);
      if($res){
        $map=array(
          'uid'=>$res,
          'user_group_id'=>$_POST["usertype"],
        );
        model("UserGroupLink")->add($map);
        if($_POST["usertype"]==2){
          $this->doUpdateUserFid($_POST["usertype"],$_POST["City"],$res);
        }
        if($_POST["usertype"]==3){
          $this->doUpdateUserFid($_POST["usertype"],$_POST["Area"],$res);
        }
        if($_POST["usertype"]==4){
          $school=array(
            'name'=>$_POST["uname"],
            'province'=>$_POST["Province"],
            'city'=>$_POST["City"],
            'area'=>$_POST["Area"],
            'type'=>$_POST["type"],
            'location'=>$_POST["schoollocation"],
            'fid'=>$res,
            'ctime'=>time(),
          );
          M("school")->add($school);
        }
        echo $res;exit();
      }else{
        echo "添加失败！";exit();
      }
    }
  //修改用户父级id
  public function doUpdateUserFid($type,$area_id,$fid){
    $map["user_group_id"]=$type==2 ? 3 : 4;
    $uid=model("UserGroupLink")->where($map)->field("uid")->select();
    $uid = getSubByKey($uid,'uid');
    $where=array(
      'uid'=>array('in' , $uid),
    );
    if($type==2){
      $where["city"]=$area_id;
    }else{
      $where["area"]=$area_id;
    }
    $data=model("User")->where($where)->field("uid")->select();
    foreach ($variable as $key => $value) {
      model("User")->where("uid=".$value["uid"])->save(array('fid'=>$fid));
    }
  }
  //修改用户
  public function doUpdateUser(){
    $uid=intval($_POST["uid"]);
    $userGroup=model("UserGroupLink")->where("uid=".$uid)->find();
    if($userGroup['user_group_id']!=4){
      $email=getAllLetter($_POST["uname"]);
    }
    if(model("User")->where("permissions=1 and email='".$email."'")->find()){
      $email=$email."_1";
    }
    $login_salt=rand(10000, 99999);
    $type=$_POST["type"];
    $data=array(
      'uname'=>$_POST["uname"],
      'pwd'=>$_POST["pwd"],
      'login_salt'=>$login_salt,
      'password'=>md5 (md5($_POST["pwd"]).$login_salt),
    );
    $res=M("User")->where("uid=".$uid)->save($data);
    if($res){
      if($type){
        M("school")->where("fid=".$uid)->save(array('type'=>$type));
      }
      echo $res;exit();
    }else{
      echo "操作失败，请重试";exit();
    }
  }
  //修改用户密码
  public function doUpdateUserPwd(){
    $uid=intval($_POST["uid"]);
    $login_salt=rand(10000, 99999);
    $data=array(
      'pwd'=>$_POST["pwd"],
      'login_salt'=>$login_salt,
      'password'=>md5 (md5($_POST["pwd"]).$login_salt),
    );
    $res=M("User")->where("uid=".$uid)->save($data);
    if($res){
      echo $res;exit();
    }else{
      echo "修改失败，请重试";exit();
    }
  }
  public function delUser(){
    $id=intval($_POST["id"]);
    //删除相应的学生
    $school=M("school")->where("fid=".$id)->find();
    $stu_ids=M("student")->where("school_id=".$school["id"])->field("stu_id")->select();
    if($stu_ids){
      foreach ($stu_ids as $key => $value) {
        $test_data=M("test_data")->where("is_del=0 and stu_id=".$value["stu_id"])->select();
        if($test_data){
          foreach ($test_data as $k => $v) {
            M("report_data")->where("tid=".$v["test_id"])->delete();
            M("test_data")->where("test_id=".$v["test_id"])->delete();
          }
        }
        M("health_report")->where("type=5 and fid=".$value["stu_id"])->delete();
        M("student")->where("stu_id=".$value["stu_id"])->delete();
      }
    }
    //删除相应的班级信息
    $class=M("class")->where("school_id=".$school["id"])->field("class_id,admin_id")->select();
    if($class){
      foreach ($class as $key => $value) {
        $health_report=M("health_report")->where("type=4 and fid=".$value["admin_id"])->select();
        if($health_report){
          foreach ($health_report as $k => $v) {
            M("report_data")->where("rpt_id=".$v["rpt_id"])->delete();
            M("health_report")->where("rpt_id=".$v["rpt_id"])->delete();
          }
        }
        M("class")->where("class_id=".$value["class_id"])->delete();
        M("user")->where("uid=".$value["admin_id"])->delete();
        M("user_group_id")->where("uid=".$value["admin_id"])->delete();
      }
    }
    //删除相应的报告信息
    $health_report=M("health_report")->where("type=3 and fid=".$id)->select();
    if($health_report){
      foreach ($health_report as $key => $value) {
        M("report_data")->where("rpt_id=".$value["rpt_id"])->delete();
        M("health_report")->where("rpt_id=".$value["rpt_id"])->delete();
      }
    }
    M("school")->where("fid=".$id)->delete();
    M("user_group_id")->where("uid=".$id)->delete();
    $res=M("User")->where("uid=".$id)->delete();
    if($res){
      echo $res;exit();
    }else{
      echo "删除失败，请重试！";exit();
    }
  }
  public function getUserList($areaId,$uname,$type){
    $map["permissions"]=1;
    $map["is_del"]=0;
    if($type==1){
      $where["user_group_id"]=2;
      if($areaId){
        $map["city"]=$areaId;
      }
    }else if($type==2){
      $where["user_group_id"]=3;
      if($areaId){
        $map["city"]=$areaId;
      }
    }else{
      $where["user_group_id"]=4;
      if($areaId){
        $map["area"]=$areaId;
      }
    }
    if($uname){
      $map["uname"]=array('like','%' . $uname. '%' );
    }
    $uid = M("UserGroupLink")->where($where)->select();
    $uid = getSubByKey($uid,'uid');
    $map["uid"]=array('in' , $uid);
    $data=M("user")->where($map)->field("uid,uname,pwd,email")->findPage(20);
    return $data;
  }
}